package com.vehicle_information_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehicle_information_management_system.dto.TripDto;
import com.vehicle_information_management_system.pojo.TripInfo;
import com.vehicle_information_management_system.service.TripInfoService;
import com.vehicle_information_management_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/tripInfo")
public class TripInfoController {
    @Autowired
    private TripInfoService tripInfoService;
    @RequestMapping("/list")
    public Result<?> getTripList(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(defaultValue = "") String search){

        IPage<TripInfo> page=tripInfoService.getTripList(pageNum,pageSize,search);
        return Result.success(page);
    }
    @RequestMapping("/add")
    public Result<?> add(@RequestBody TripInfo tripInfo){
        try{
            tripInfoService.add(tripInfo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success("添加成功");
    }
    @RequestMapping("/update")
    public Result<?> update(@RequestBody TripInfo tripInfo){
        try{
            tripInfoService.update(tripInfo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success("修改成功");
    }
    @RequestMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id){
        try{
            tripInfoService.delete(id);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success("删除成功");
    }

    @RequestMapping("/getTripListByUserId")
    public Result<?> getTripListByUserId(@RequestParam(defaultValue = "1") Integer pageSize,
                                         @RequestParam(defaultValue = "10") Integer pageNum,
                                         @RequestParam Integer userId){
        try{
            IPage<TripDto> page= tripInfoService.getTripListByUserId(pageSize, pageNum, userId);
            return Result.success(page);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}
